
import store from '@/store';
import {isApp} from '@/lib/judge-env';


const isAppBoolen = isApp();

/**
 * @typedef {(res: import('.').normalPortalResponse, payload: payloadDataSet) => void} ResponseHandler
 */

/**
 * @type {Record<string, ResponseHandler>}
 */

/**
 * 防抖错误处理
 * @type {ResponseHandler}
 */
export function dataBouncing(res, payload) {

}

/**
 * 请求结果正常
 * @type {ResponseHandler}
 */
export function success() {

}

/**
 * 账号互顶
 * @type {ResponseHandler}
 */
export function otherLogins(res, payload) {
     const { desc } = res.status || {};
     let pageMessage = {
        code: 100000,
        interfaceCode: 100000,
        message: desc || '',
        btnShow: true,
        buttonLabel: '退出'
     }
     store.commit('meta', {
        initialized: false,
        pageMessage
     });
}

/**
 * 未登录错误码
 * @type {ResponseHandler}
 */
export function notLogin(res, payload) {
    if (isAppBoolen) { // 在app里面
        let pageMessage = {
            code: 405,
            interfaceCode: 20100001,
            message: `对不起，您还未加入该站点${process.env.NODE_ENV === 'development' ? '：' + window.location.href : ''}`,
        }
        store.commit('meta', {
            initialized: false,
            pageMessage
        });
    } else {
        // 用户登录失效
        store.dispatch('logout')
    }
}

/**
 * 账号被禁用
 * @type {ResponseHandler}
 */
export function accountDisabled(res, payload) {
     const { desc } = res.status || {};
     let pageMessage = {
        code: 30020017,
        interfaceCode: 30020017,
        message: desc || '',
        btnShow: true,
        buttonLabel: '退出'
     }
     store.commit('meta', {
        initialized: false,
        pageMessage
     });
}

/**
 * 非白名单用户登录
 * @type {ResponseHandler}
 */
export function systemRefused(res, payload) {
    const { desc } = res.status || {};
    let pageMessage = {
        code: 403,
        interfaceCode: 30020022,
        message: desc || '',
        btnShow: true
    }
    if(isAppBoolen) {
        pageMessage.buttonLabel = '返回上一页';
    } else {
        pageMessage.buttonLabel = '退出';
    }
    store.commit('meta', {
        initialized: false,
        pageMessage
    });
}

/**
 * 服务器异常：似乎出了什么状态，正在恢复中...
 * @type {ResponseHandler}
 */
export function serverError(res, payload) {
    G_dispatchEvent('toast', { message: '服务开小差了，请稍后再试～', type: 'error' });
}

/**
 * 未知错误
 * @type {ResponseHandler}
 */
export function unknownError(res, payload) {
    G_dispatchEvent('toast', { message: '服务开小差了，请稍后再试～', type: 'error' });
}



export function errorHandler(pageMessage) {
	if(!isAppBoolen) {// 不是在app里面
		switch(pageMessage.interfaceCode) {
			case 30020022:
				store.dispatch('logout');
				break;
            default: {
                
                break;
            }
		}
	} else {
        store.dispatch('jsBridge/disposeBridge', {
            isBack: true,
            data: {
                refresh: true
            }
        });
		// switch(pageMessage.interfaceCode) {
		// 	case 30020022:
		// 		store.dispatch('jsBridge/disposeBridge', {
		// 			isBack: true,
		// 			data: {
		// 				refresh: true
		// 			}
		// 		});
		// 	    break;
        //     default: {
        //         store.dispatch('jsBridge/disposeBridge', {
        //             method: 'goNewPage',
        //             url: {
        //                 ...pageMessage.target
        //             }
        //         })
        //         break;
        //     }
		// }
	}
}